#import "HLayoutView.h"

@implementation HLayoutView

- (id)initWithFrame:(CGRect)frame spacing:(int)s leftMargin:(int)lm
    rightMargin:(int)rm topMargin:(int)tm bottomMargin:(int)bm
    alignment:(UIControlContentHorizontalAlignment)a;
{
    if (self = [super initWithFrame:frame spacing:s leftMargin:rm rightMargin:rm
                topMargin:tm bottomMargin:bm])
    {
        if (a == UIControlContentHorizontalAlignmentFill)
            a = UIControlContentHorizontalAlignmentCenter;
        alignment = a;
    }
    return self;
}

- (CGSize)layoutSubviewsEffectively:(BOOL)effectively
{
    switch (alignment)
    {
        case UIControlContentHorizontalAlignmentLeft:
        {
            int left = leftMargin, top = topMargin, max_bottom = 0;
            for (int i = 0; i < [self.subviews count]; ++i)
            {
                UIView *child = [self.subviews objectAtIndex:i];
                if (i != 0)
                    left += spacing;
                if (effectively)
                    child.frame = CGRectMake(left, top, child.frame.size.width,
                                             child.frame.size.height);
                left += child.frame.size.width;
                if (max_bottom < top + child.frame.size.height)
                    max_bottom = top + child.frame.size.height;
            }
            return CGSizeMake(left + rightMargin, max_bottom + bottomMargin);
        }
        case UIControlContentHorizontalAlignmentRight:
        {
            int right = self.frame.size.width - rightMargin, top = topMargin,
                max_bottom = 0;
            for (int i = [self.subviews count] - 1; i >= 0; --i)
            {
                UIView *child = [self.subviews objectAtIndex:i];
                if (i != [self.subviews count] - 1)
                    right -= spacing;
                if (effectively)
                    child.frame = CGRectMake(right - child.frame.size.width,
                                             top, child.frame.size.width,
                                             child.frame.size.height);
                right -= child.frame.size.width;
                if (max_bottom < top + child.frame.size.height)
                    max_bottom = top + child.frame.size.height;
            }
            return CGSizeMake(self.frame.size.width - right + leftMargin,
                              max_bottom + bottomMargin);
        }
        default: // center
        {
            int total_width = leftMargin;
            for (UIView *child in self.subviews)
                total_width += child.frame.size.width;
            total_width += ([self.subviews count] - 1) * spacing + rightMargin;
            int left = self.frame.size.width / 2 - total_width / 2 + leftMargin,
                top = topMargin, max_bottom = 0;
            for (int i = 0; i < [self.subviews count]; ++i)
            {
                UIView *child = [self.subviews objectAtIndex:i];
                if (i != 0)
                    left += spacing;
                if (effectively)
                    child.frame = CGRectMake(left, top, child.frame.size.width,
                                             child.frame.size.height);
                left += child.frame.size.width;
                if (max_bottom < top + child.frame.size.height)
                    max_bottom = top + child.frame.size.height;
            }
            return CGSizeMake(total_width, max_bottom + bottomMargin);
        }
    }
}

@end
